/******************************************************************************
 * @File name   :      MaintainEmployee.java
 *
 * @Author      :      Panxing Wu
 *
 * @Date        :      2011-6-27
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2011-6-27 上午11:15:07        Panxing Wu     1.0            Initial Version
 * 2011-9-27 pm17:49:07        Panxing Wu     1.1       add validateCertificateCode
 *****************************************************************************/
package com.cap.bts.framework.dm.employeemgnt.contorller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cap.bts.framework.codemapping.domain.CodeInfo;
import com.cap.bts.framework.codemapping.service.ICodeMapping;
import com.cap.bts.framework.common.constant.CodeMappingCode;
import com.cap.bts.framework.common.constant.CodeMappingType;
import com.cap.bts.framework.common.constant.CommonConstants;
import com.cap.bts.framework.common.controller.BaseController;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.CodeTextUtils;
import com.cap.bts.framework.common.util.DateConvert;
import com.cap.bts.framework.dm.employeemgnt.dto.MaintainEmployeeResultDto;
import com.cap.bts.framework.dm.employeemgnt.dto.MaintainEmployeeSubmitDto;
import com.cap.bts.framework.dm.employeemgnt.service.IMaintainDealerEmployeeService;
import com.cap.bts.framework.dm.employeemgnt.utils.EmployeeMgntUtil;
import com.cap.bts.framework.usermgnt.constant.EmployeeStatus;
import com.cap.bts.framework.usermgnt.domain.User;
import com.cap.bts.framework.usermgnt.dto.ApprovalSubmitDto;
import com.cap.bts.framework.usermgnt.dto.QueryEmployeeDto;


/**
 *
 */
@Controller
@RequestMapping(value = "/dm/dealer/employeemgnt")
public class MaintainEmployeeController  extends BaseController{
	
	static final String getMaintainEmployeePage="getMaintainEmployeePage";

    @Autowired
    IMaintainDealerEmployeeService maintainDealerEmployeeService;

    @Autowired
    ICodeMapping codeMappingService;
    

    /**
     * 维护人员首页查询
     * 
     * @Date : 2011-7-6
     * @param dto
     *            QueryEmployeeDto
     * @param model
     *            Model
     * @param request
     *            HttpServletRequest
     * @return String
     * @throws AppException
     */
    @RequestMapping(value = "/toMaintainEmployeePage")
    public String getMaintainEmployeePage(QueryEmployeeDto dto, Model model,
        HttpServletRequest request) throws AppException {
        User currenUser = this.getCurrentUser();
        List<MaintainEmployeeResultDto> employeeResultDtoList = new ArrayList<MaintainEmployeeResultDto>();
        if (dto.getName() != null) {
            dto.setBrandId(currenUser.getBrandId());  
            dto.setDealerId(this.getDealerId());
            dto.setPaging(true);
            employeeResultDtoList = maintainDealerEmployeeService
                .getMaintainDealerEmployeeByDto(dto);
        }
        request.getSession().setAttribute(getMaintainEmployeePage, dto);
        model.addAttribute(CommonConstants.REQUEST_ATTRIBUTE_PAGER, dto.getPager());
        model.addAttribute("empResultDtoList", employeeResultDtoList);
        model.addAttribute("dto", dto);
        model.addAttribute("gender",codeMappingService.getCodeInfoByType(CodeMappingType.GENDER.getValue()));
        return "/dm/employeemgnt/maintainDmsEmployee";

    }

    /**
     * 转向新增人员页面
     * 
     * @Date : 2011-7-6
     * @param model
     *            Model
     * @param request
     *            HttpServletRequest
     * @return String
     * @throws AppException
     */
    @RequestMapping(value = "/createEmployeePage")
    public String getCreateEmployeePage(Model model, HttpServletRequest request)
        throws AppException {
        MaintainEmployeeSubmitDto mEmployeeSubmitDto = new MaintainEmployeeSubmitDto();
        CodeInfo idCardCodeInfo = codeMappingService.getCodeInfoByCode(CodeMappingCode.ID_CARD_CODE.getValue());
        model.addAttribute("idCardCodeInfo", idCardCodeInfo);
        model.addAttribute("submitDto", mEmployeeSubmitDto);
        model.addAttribute("gender",
            codeMappingService.getCodeInfoByType(CodeMappingType.GENDER.getValue()));
        model.addAttribute("education", codeMappingService.getCodeInfoByType(String
            .valueOf(CodeMappingType.EDUCATION.getValue())));
        model.addAttribute("certificateType", codeMappingService.getCodeInfoByType(String
            .valueOf(CodeMappingType.CERTIFICATE_TYPE.getValue())));
        model.addAttribute("marriage", codeMappingService.getCodeInfoByType(String
            .valueOf(CodeMappingType.MARRIAGE.getValue())));
        model.addAttribute("ownerId", this.getDealerCode()); 
        model.addAttribute("exportDataMap", CodeTextUtils.getStatusMap("EmployeeInfo.ExportData"));
        model.addAttribute("currentDate", DateConvert.DateFormat(new Date(), "yyyy-MM-dd"));
        return "/dm/employeemgnt/createDmsEmployee";

    }

    /**
     * 保存人员 信息
     * 
     * @Date : 2011-7-6
     * @param mEmployeeSubmitDto
     *            MaintainEmployeeSubmitDto
     * @param model
     *            Model
     * @param request
     *            HttpServletRequest
     * @return String
     * @throws AppException
     * @throws IOException
     */
    @RequestMapping(value = "/saveEmployee")
    public String submitToSaveEmployee(MaintainEmployeeSubmitDto mEmployeeSubmitDto, Model model,
        HttpServletRequest request) throws AppException, IOException,Exception {
        Object obj = request.getSession().getAttribute("userSession");
        User user = (obj == null ? new User() : (User) obj);
        mEmployeeSubmitDto.setDealerId(this.getDealerId());
        try {
        	mEmployeeSubmitDto.setEmployeeCode(EmployeeMgntUtil.createEmployeeCode());
            maintainDealerEmployeeService.saveEmployee(mEmployeeSubmitDto, user, this.getDealerId(), this.getDealerCode());
        } catch (Exception e) {
           throw e;
        }
        QueryEmployeeDto queryDto = new QueryEmployeeDto();
        queryDto.setName("");
        Object queryDtoObject=request.getSession().getAttribute(getMaintainEmployeePage);
        if(queryDtoObject!=null){
        	queryDto=(QueryEmployeeDto)queryDtoObject;
        }
        return getMaintainEmployeePage(queryDto, model, request);
    }

    
    /**
     * 返回维护人员查询
     * @Date        :      2011-6-23
     * @param submitEmployeeDto
     * @param model
     * @return
     * @throws AppException
     */
    @RequestMapping(value = "/returnToEmployeeSearch", method = RequestMethod.GET)
    public String returnToEmployeeSearch(HttpServletRequest request,Model model) throws AppException{
                
    	QueryEmployeeDto queryDto = new QueryEmployeeDto();
        queryDto.setName("");
        Object queryDtoObject=request.getSession().getAttribute(getMaintainEmployeePage);
        if(queryDtoObject!=null){
        	queryDto=(QueryEmployeeDto)queryDtoObject;
        }
        return getMaintainEmployeePage(queryDto, model, request);
        
    }

    /**
     * 重置密码为六个1
     * 
     * @Date : 2011-6-29
     * @param userCode
     *            string
     * @param model
     *            Model
     * @param response
     *            HttpServletResponse
     * @return null
     * @throws AppException
     * @throws IOException
     */
    @RequestMapping(value = "/resetpassword")
    @ResponseBody
    public String resetpassword(@RequestParam("userCode") String userCode) throws AppException{
        
        User currentUser = this.getCurrentUser();
        maintainDealerEmployeeService.resetpassword(userCode, currentUser);
        return "1";
    }



    /**
     * 删除人员信息
     * 
     * @Date : 2011-6-30
     * @param employeeId
     *            long
     * @param model
     *            Model
     * @param response
     *            HttpServletResponse
     * @return null
     * @throws AppException
     * @throws IOException
     */
    @RequestMapping(value = "/deleteEmployee")
    @ResponseBody
    public String deleteEmployee(@RequestParam("employeeId") long employeeId) throws AppException {

        maintainDealerEmployeeService.deleteEmployee(employeeId);
        return "1";

    }
    
    /**
     * 验证身份证号
     * @Date        :      2011-9-27
     * @param certificateCode
     * @return
     * @throws AppException
     */
    @RequestMapping(value = "/validatecertificatecode") 
    @ResponseBody
    public String validateCertificateCode(@RequestParam("certificatecode") String certificateCode) throws AppException{
        
        String  result = maintainDealerEmployeeService.validateCertificateCode(certificateCode, null);
        return result;
    }
    /**
     * 更新人员验证证件是否重复
     * @Date        :      Aug 8, 2012
     * @param certificateCode
     * @param certificateType
     * @param userCode
     * @return
     * @throws AppException
     */
    @RequestMapping(value = "/validatecertificatecodeForUpdate") 
    @ResponseBody
    public String validateCertificateCode(@RequestParam("certificatecode") String certificateCode
    		,@RequestParam("certificateType") Long certificateType
    		,@RequestParam("userCode") String userCode) throws AppException{    	
    	boolean  result = maintainDealerEmployeeService.validateCertificateCodeForUpdate(certificateCode,certificateType,userCode);
    	return String.valueOf(result);
    }
     
    
    
    
    /**
     * 验证雇员状态
     * @Date        :      2012-7-26
     * @param certificateCode
     * @return
     * @throws AppException
     */
    @RequestMapping(value = "/validateEmployeeStatus") 
    @ResponseBody
    public String validateEmployeeStatus(@RequestParam("employeeId") long employeeId) throws AppException{
        
    	MaintainEmployeeSubmitDto  result = maintainDealerEmployeeService.getMaintainDealerEmployeeById(employeeId);
    	if(result!=null && result.getEmploymentStatusId()!=null){
    		if(EmployeeStatus.PEND_TO_CREATE==result.getEmploymentStatusId().longValue() || EmployeeStatus.PEND_TO_CHANGE==result.getEmploymentStatusId().longValue()){
    			return "true";
    		}
    	}
        return "false";
    }
    
    @RequestMapping(value = "/toApproveEmployeePage")
    public String toApproveEmployeePage(QueryEmployeeDto dto, Model model,
        HttpServletRequest request) throws AppException {
        User currenUser = this.getCurrentUser();  
        dto.setEmployeeStatusStr("3,5");
        List<MaintainEmployeeResultDto> employeeResultDtoList = new ArrayList<MaintainEmployeeResultDto>();
        if (dto.getName() != null) {
            dto.setBrandId(currenUser.getBrandId());              //dto.setDealerId(this.getDealerId());
            dto.setPaging(true);
            employeeResultDtoList = maintainDealerEmployeeService
                .getMaintainDealerEmployeeByDto(dto);
        }
        request.getSession().setAttribute(getMaintainEmployeePage, dto);
        model.addAttribute(CommonConstants.REQUEST_ATTRIBUTE_PAGER, dto.getPager());
        model.addAttribute("empResultDtoList", employeeResultDtoList);
        model.addAttribute("dto", dto);
        model.addAttribute("gender",
            codeMappingService.getCodeInfoByType(CodeMappingType.GENDER.getValue()));
        
        return "/dm/employeemgnt/maintainApproveDealerEmployee";

    }
    
    @RequestMapping(value = "/approveUserGroupApplication", method = RequestMethod.POST)
    public String approveUserGroupApplication(ApprovalSubmitDto submitDto,
        HttpServletRequest request,Model model) throws AppException {
    	maintainDealerEmployeeService.approveUserGroupApplication(submitDto);
      return this.toApproveEmployeePage(new QueryEmployeeDto(), model, request);
    }
    
    

    
}
